﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections.ObjectModel;

namespace RollCallClient
{
    public partial class InfoSetting : Form
    {
        UserInfo USInfo;
        Collection<int> clCheckedDates = new Collection<int>();
        
        
        public InfoSetting()
        {
            InitializeComponent();
            USInfo = new UserInfo();
            USInfo.ReadUserInfoFromFile();
            this.txt_Username.Text = USInfo.GetUserName();
            SetDayCheck(USInfo.GetdayChecking());

        }
        /// <summary>
        /// use to set day check
        /// </summary>
        /// <param name="days"></param>
        public void SetDayCheck(Collection<int> clDates)
        {
            foreach (int intDate in clDates)
            {
                switch (intDate)
                {
                    case 0: { chk_Sun.Checked = true; break; }
                    case 1: { chk_Mon.Checked = true; break; }
                    case 2: { chk_Tue.Checked = true; break; }
                    case 3: { chk_Wen.Checked = true; break; }
                    case 4: { chk_Thus.Checked = true; break; }
                    case 5: { chk_Fri.Checked = true; break; }
                    case 6: { chk_Sat.Checked = true; break; }

                }
            }
        }

        /// <summary>
        /// use to validate user name 
        /// </summary>
        /// <param name="user">name of user</param>
        /// <returns></returns>
        public bool ValidateUserName(String strUserName)
        {
            String User = strUserName.Trim();
            if (User.Length == 0)
            {
                MessageBox.Show("You must enter Username", "Warring!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }
        /// <summary>
        /// use to canncel setting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        /// <summary>
        /// check select
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void chk_CheckedChanged(object sender, EventArgs e)
        {
            if (chk.Checked == true)
            {
                chk_Mon.Checked = true;
                chk_Tue.Checked = true;
                chk_Wen.Checked = true;
                chk_Thus.Checked = true;
                chk_Fri.Checked = true;
                chk_Sat.Checked = true;
                chk_Sun.Checked = true;
            }
            else
            {
                chk_Mon.Checked = false;
                chk_Tue.Checked = false;
                chk_Wen.Checked = false;
                chk_Thus.Checked = false;
                chk_Fri.Checked = false;
                chk_Sat.Checked = false;
                chk_Sun.Checked = false;
            }
        }
        /// <summary>
        /// save setting
        /// 83f989dd820bb3683ef6ff6b2bc7fd68 is used to make sure user can setting once time
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Save_Click_1(object sender, EventArgs e)
        {
             string strInput = "";
             string strFilePath = Application.StartupPath;
            strFilePath = strFilePath + "\\app.dat";
            try
            {
                StreamReader srFileReader = File.OpenText(strFilePath);
                strInput = srFileReader.ReadLine();
                srFileReader.Close();
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (!strInput.Equals("83f989dd820bb3683ef6ff6b2bc7fd68"))
            {
                DialogResult dlgResult = MessageBox.Show("You will be checked as \"" + txt_Username.Text.ToUpper() + " \"" + "\nRemember that it can't be changed afterward!",
                "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dlgResult == DialogResult.No)
                {
                    txt_Username.Focus();
                }
                else
                {
                    NoRepick();
                    String strUserName = txt_Username.Text.Trim();

                    if (chk_Mon.Checked)
                    {
                        clCheckedDates.Add(1);
                    }
                    if (chk_Tue.Checked)
                    {
                        clCheckedDates.Add(2);
                    }
                    if (chk_Wen.Checked)
                    {
                        clCheckedDates.Add(3);
                    }
                    if (chk_Thus.Checked)
                    {
                        clCheckedDates.Add(4);
                    }
                    if (chk_Fri.Checked)
                    {
                        clCheckedDates.Add(5);
                    }
                    if (chk_Sat.Checked)
                    {
                        clCheckedDates.Add(6);
                    }
                    if (chk_Sun.Checked)
                    {
                        clCheckedDates.Add(0);
                    }
                    if (ValidateUserName(strUserName))
                    {
                        try
                        {
                            USInfo.SetUserName(strUserName);
                            USInfo.SetDayChecking(clCheckedDates);
                            USInfo.WriteUserInfoToFile();
                        }
                        catch (IOException ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        this.Dispose();
                    }
                }
            }
        }
        /// <summary>
        /// Use to know the firt time setting
        /// 83f989dd820bb3683ef6ff6b2bc7fd68 is used to make sure user can setting one time
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InfoSetting_Load(object sender, EventArgs e)
        {
            
            string strFilePath = Application.StartupPath;
            strFilePath = strFilePath + "\\app.dat";
            string strInput = "";
            try
            {
                StreamReader srFileReader = File.OpenText(strFilePath);
                strInput = srFileReader.ReadLine();
                srFileReader.Close();
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
            if (strInput.Equals("83f989dd820bb3683ef6ff6b2bc7fd68"))
            {
                txt_Username.Enabled = false;
            }
        }

        /// <summary>
        /// forbid setting
        ///83f989dd820bb3683ef6ff6b2bc7fd68 is used to make sure user can setting one time
        /// </summary>
        public void NoRepick()
        {
            string strFilePath = Application.StartupPath;
            strFilePath = strFilePath + "\\app.dat";
            try
            {
                using (System.IO.StreamWriter swFileWriter = new System.IO.StreamWriter(strFilePath))
                {
                    swFileWriter.WriteLine("83f989dd820bb3683ef6ff6b2bc7fd68");
                }
            }
            catch (IOException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }

}
